package com.rover.service.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grid.Page;
import com.rover.dao.BaseDao;
import com.rover.entity.system.Code;

@Service
@Transactional
public class CodeService {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private BaseDao<Code> codeDao;


	@Transactional
	public Page<Code> query(Page<Code> page, String strWhere,
			Map<String, Object> params) {

		String hql = "from Code " + strWhere;
		List<Code> li=codeDao.find(hql, params, page.getPageNo(),
				page.getPageSize());
		page.setResult(li);
		String totalHql = "select count(*) " + hql;
		page.setTotalCount(codeDao.count(totalHql, params));
		return page;
		// return
		// userDao.find("select new Tuser(t.id,t.name,t.createdatetime,t.modifydatetime) from Tuser t ");
	}

	@Transactional
	public void save(Code code) {
		codeDao.saveOrUpdate(code);
	}
	@Transactional
	public void delete(Long id) {
		codeDao.delete(Code.class,id);
	}
	
	
	/*
	 * 添加的时候判断名称是否重复
	 * @name 要判断的名称
	 * */
	public Boolean isAddNameRepeat(String name){
		String hql="select count(id) from Code where name=:name";
		 Map<String, Object> params=new HashMap<String, Object>();
		 params.put("name", name);
		if(codeDao.count(hql, params)>0){
			return true;
		}
		else
			return false;
	}
	/*
	 * 编辑的时候判断名称是否重复
	 * @name 要判断的名称
	 * */
	public Boolean isEditNameRepeat(Long id,String name){
		String hql="select count(id) from Code where id<>:id and name=:name";
		 Map<String, Object> params=new HashMap<String, Object>();
		 params.put("id", id);
		 params.put("name", name);
		if(codeDao.count(hql, params)>0){
			return true;
		}
		else
			return false;
	}
	
}
